package com.example.service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.example.entity.CircleMember;
import com.example.entity.CircleShareEntity;
import com.example.entity.ShareEntity;
import com.example.huashangapp.HSApplication;
import com.example.service.Mp3Service.MyBinder;
import com.example.ui.Userdata;
import com.example.ui.learn.DownUtils;
import com.example.ui.leftmenu.Circle;
import com.example.util.MyEntity;
import com.example.util.Tools;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class MainService extends Service {
	public static Context context;
	public static boolean isWIFI  = true;
	public static List<ShareEntity> shareEntity;
	public static List<ShareEntity> newShareEntity;
	public static Circle circle;
	public static CircleMember circleMember;
	public static Mp3Service.MyBinder  myBinder;
	public static Mp3Service.MyBinder getMyBinder() {
		return myBinder;
	}

	public static void setMyBinder( Mp3Service.MyBinder myBinder) {
		MainService.myBinder = myBinder;
	}
	public static Thread thread;
	public static boolean isAutoLogin = false;
	public static boolean isLeftMenu = false;
	private Intent intentMP3;
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		context=this.getBaseContext();
		intentMP3= new Intent("com.huashang.mp3_service");
		bindService(intentMP3,MainService.conn, Service.BIND_AUTO_CREATE);
		super.onCreate();
	}
	/**
	 * 设置手机网络类型，wifi，cmwap，ctwap，用于联网参数选择
	 * 
	 * @return
	 */
	@SuppressLint("DefaultLocale")
	public static String setNetworkType(final Context context) {
		
		String netType = "";  

        ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 

        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 

         

        if(networkInfo==null){ 

            return netType; 

        } 

        int nType = networkInfo.getType(); 

        if(nType==ConnectivityManager.TYPE_MOBILE){ 

            Log.e("networkInfo.getExtraInfo()", "networkInfo.getExtraInfo() is "+networkInfo.getExtraInfo()); 

            if(networkInfo.getExtraInfo().toLowerCase().equals("cmnet")){ 

                netType = "cmnet"; 

            } 

            else{ 

                netType = "cmwap"; 

            } 

        } 

        else if(nType==ConnectivityManager.TYPE_WIFI){ 

            netType = "wifi"; 

        } 
        Log.i("msg","您的网络类型"+netType);
        return netType; 
	}
	static Intent intent=new Intent();
	public static void OpenPhoneListen(final Context context){
		// 创建一个电话服务
		 TelephonyManager manager = (TelephonyManager)context.getSystemService(TELEPHONY_SERVICE);
			// 监听电话状态，接电话时停止播放
			manager.listen(new PhoneStateListener(){
				Boolean isCalling=false;
				public void onCallStateChanged(int state, String incomingNumber) {
					if(HSApplication.HSlistInfo.getListen_uri()!=null&&!"".equals(HSApplication.HSlistInfo.getListen_uri())){
						intent.putExtra("musicId",HSApplication.HSlistInfo.getListen_id());
						intent.putExtra("uri",HSApplication.HSlistInfo.getListen_uri());
					}
				switch (state) {
					case TelephonyManager.CALL_STATE_IDLE:
						intent.putExtra("mode", Tools.PLAY);
						if(myBinder!=null&isCalling){
							myBinder.startMp3Service(intent);
							isCalling=false;
						}
						break;
					case TelephonyManager.CALL_STATE_RINGING:
						intent.putExtra("mode", Tools.PAUSE);
						if(myBinder!=null&&Mp3Service.mp.isPlaying()){
							myBinder.startMp3Service(intent);
							isCalling=true;
						}
						break;
					case TelephonyManager.CALL_STATE_OFFHOOK:
						if(myBinder!=null&&Mp3Service.mp.isPlaying()){
							intent.putExtra("mode", Tools.PAUSE);
							myBinder.startMp3Service(intent);
							isCalling=true;
						}
					default:
						break;
					}
				}
			},
			PhoneStateListener.LISTEN_CALL_STATE);
	}
	/*
	 * 监听电话状态
	 */
	public static ServiceConnection conn=new ServiceConnection() {
		@Override
		public void onServiceDisconnected(ComponentName name) {
			if(myBinder!=null){
				intent.putExtra("mode", Tools.STOP);
				MainService.myBinder.startMp3Service(intent);
			}
		}
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			setMyBinder((Mp3Service.MyBinder) service);
		}
	};

}
